package com.hxexp.weixin.mvc.common.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.hxexp.weixin.common.exception.APIException;
import com.hxexp.weixin.constant.StartStationsId;
import com.hxexp.weixin.constant.TrafficlineNC;
import com.hxexp.weixin.constant.WebAppVersion;
import com.hxexp.weixin.hpe.api.startstations.StartStationsAPI;
import com.hxexp.weixin.hpe.api.startstations.model.StartStationsBean;
import com.hxexp.weixin.hpe.api.startstations.model.StartStationsParamBean;
import com.hxexp.weixin.hpe.api.startstations.model.StartStationsResultBean;
import com.hxexp.weixin.hpe.api.startstations.model.StationBean;
import com.hxexp.weixin.mvc.common.service.CommonSerivce;
import com.hxexp.weixin.mvc.order.model.OrderConfirmInfoBean;

/**
 * 公共方法服务实现类
 *
 * @author li.jiang
 *
 */
@Service
public class CommonSerivceImpl implements CommonSerivce {
    @Autowired
    private StartStationsAPI startStationsAPI;
    // 仪陇超长客运站的汉中到达站
    private static final String YLCC_HANZHONG_ENDSTATION_ID = "HANZHONG";
    // 仪陇超长客运站的西安到达站
    private static final String YLCC_XIAN_ENDSTATION_ID = "XIAN";
    @Value("${no_service_price}")
    private String noServicePriceStartStationIds;
    /**
     * 确定指定的出发站是否是当代集团的车站
     *
     * @param startStationsResult
     * @return true: 当代集团的车站，false: 不是当代集团的车站
     */
    @Override
    public boolean isNCStartStation(String startCityId, String startStationId) {
        StartStationsResultBean startStationsResult = new StartStationsResultBean();
        try {
            startStationsResult = startStationsAPI.getStartStations(new StartStationsParamBean(TrafficlineNC.GROUP_ID));
        } catch (APIException e) { // NOSONAR
            // do nothing
        }

        for (StartStationsBean startStation : startStationsResult.getStartStations()) {
            if (startCityId != null && startCityId.equals(startStation.getCityId())) {
                return true;
            }

            if (startStationId != null) {
                for (StationBean station : startStation.getStations()) {
                    if (startStationId.equals(station.getStationId())) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.hxexp.weixin.mvc.common.service.CrossProvinceSerivce#isPrivilege(com.hxexp.weixin.mvc.order.model.
     * OrderConfirmInfoBean)
     */
    @Override
    public boolean isPrivilege(OrderConfirmInfoBean orderConfirmInfo) {
        boolean isNonPrivilegeLine = StartStationsId.YLCCX.equals(orderConfirmInfo.getStartStationId())
                && YLCC_HANZHONG_ENDSTATION_ID.equals(orderConfirmInfo.getEndStationId())
                || StartStationsId.YLCCX.equals(orderConfirmInfo.getStartStationId())
                && YLCC_XIAN_ENDSTATION_ID.equals(orderConfirmInfo.getEndStationId());
        return isNCStartStation("", orderConfirmInfo.getStartStationId()) && !isNonPrivilegeLine;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.hxexp.weixin.mvc.common.service.CrossProvinceSerivce#isDYStartStation(java.lang.String)
     */
    @Override
    public boolean isDYStartStation(String startStationId) {
        StartStationsResultBean startStationsResult = new StartStationsResultBean();
        try {
            startStationsResult = startStationsAPI.getStartStations(new StartStationsParamBean(WebAppVersion.DY_GROUPID));
        } catch (APIException e) { // NOSONAR
            // do nothing
        }

        for (StartStationsBean startStation : startStationsResult.getStartStations()) {
            for (StationBean station : startStation.getStations()) {
                if (startStationId.equals(station.getStationId())) {
                    return true;
                }
            }
        }

        return false;
    }

    @Override
    public boolean isXAStartStation(String startCityId, String startStationId) {
        return WebAppVersion.CITY_ID_XA.equals(startCityId) || StartStationsId.XA.equals(startStationId)
                || StartStationsId.XAWSL.equals(startStationId);
    }

    /*
     * (non-Javadoc)
     *
     * @see com.hxexp.weixin.mvc.common.service.CommonSerivce#isNoServicePrice(java.lang.String)
     */
    @Override
    public boolean isNoServicePrice(String startStationId) {
        List<String> noServicePriceStartStationIdList = Arrays.asList(noServicePriceStartStationIds.split(","));
        return noServicePriceStartStationIdList.contains(startStationId);
    }

    @Override
    public boolean isBYStartStation(String startStationId) {
        StartStationsResultBean startStationsResult = new StartStationsResultBean();
        try {
            startStationsResult = startStationsAPI.getStartStations(new StartStationsParamBean(WebAppVersion.BY_GROUPID));
        } catch (APIException e) { // NOSONAR
            // do nothing
        }

        for (StartStationsBean startStation : startStationsResult.getStartStations()) {
            for (StationBean station : startStation.getStations()) {
                if (startStationId.equals(station.getStationId())) {
                    return true;
                }
            }
        }

        return false;
    }

}
